local xiongtu = fk.CreateSkill {
  name = "lingling__xiongtu",
}

Fk:loadTranslationTable{
  ["lingling__xiongtu"] = "雄图",
  [":lingling__xiongtu"] = "当一名其他角色使用、弃置、被你获得武器牌后，你可以获得其一张手牌。若你因此获得武器牌，你可以视为对其使用【杀】。",

  ["#lingling__xiongtu-invoke"] = "雄图：是否获得 %dest 一张手牌？",
  ["#lingling__xiongtu-use"] = "雄图：你可以视为对 %dest 使用一张【杀】",
}

local spec = {
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if room:askToSkillInvoke(player, {
      skill_name = xiongtu.name,
      prompt = "#lingling__xiongtu-invoke::"..to.id,
    }) then
      event:setCostData(self, {tos = {to}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local card = room:askToChooseCard(player, {
      target = to,
      flag = "h",
      skill_name = xiongtu.name,
    })
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, xiongtu.name, nil, false, player)

    if Fk:getCardById(card).sub_type == Card.SubtypeWeapon then
      room:askToUseVirtualCard(player,{
        name = "slash",
        cancelable = true,
        skill_name = xiongtu.name,
        prompt = "#lingling__xiongtu-use::"..to.id,
        extra_data = {
          bypass_distances = true,
          bypass_times = true,
          exclusive_targets = {to.id}
        }
      })
    end
  end,
}

xiongtu:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(xiongtu.name) then
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and not move.from.dead and not move.from:isKongcheng() then
          if move.to == player and move.toArea == Card.PlayerHand and
            move.moveReason == fk.ReasonPrey and move.proposer == player then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
                return true
              end
            end
          end
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.from and move.from ~= player then
        if move.to == player and move.toArea == Card.PlayerHand and
          move.moveReason == fk.ReasonPrey and move.proposer == player then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
              table.insertIfNeed(targets, move.from)
            end
          end
        end
        if move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
              table.insertIfNeed(targets, move.from)
              break
            end
          end
        end
      end
    end
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      if not player:hasSkill(xiongtu.name) then return end
      if not p:isKongcheng() then
        event:setCostData(self, {tos = {p}})
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

xiongtu:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target ~= player and player:hasSkill(xiongtu.name) and
      data.card.sub_type == Card.SubtypeWeapon and not target:isKongcheng() then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

return xiongtu
